package org.whatever.aha.zjut.platform.controller.project;

import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.constant.StatusConstant;
import org.whatever.aha.zjut.base.dto.AjaxResult;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.entity.competition.CompetitionStageStatus;
import org.whatever.aha.zjut.platform.entity.project.ProjectResource;
import org.whatever.aha.zjut.platform.service.competition.CompetitionStageStatusService;
import org.whatever.aha.zjut.platform.service.project.ProjectResourceService;
import org.whatever.aha.zjut.platform.service.project.ProjectService;
import org.whatever.aha.zjut.platform.util.CheckUtil;
import org.whatever.aha.zjut.platform.vo.CosPolicyVo;
import org.whatever.aha.zjut.platform.vo.CosPostPolicyVo;
import org.whatever.aha.zjut.platform.vo.project.ProjectResourceVo;

import java.util.List;

/**
 * @author Vc
 * @version 1.0
 * @Desc
 * @date 2021/09/16 16:00
 */
@Api(tags = "项目资源相关操作类")
@RequestMapping("/v1/projectResource")
@RequiredArgsConstructor
@RestController
@Validated
public class ProjectResourceController {
    final ProjectService projectService;
    final ProjectResourceService projectResourceService;
    final CompetitionStageStatusService competitionStageStatusService;

    /**
     * 根据项目ID和赛事阶段ID获得所有的资源
     *
     * @param projectId
     * @return
     */
    @GetMapping("/{projectId}/{stageId}")
    public AjaxResult<List<ProjectResourceVo>> getAllProjectResourceByProjectId(@PathVariable("projectId") int projectId,@PathVariable("stageId") int stageId) {
        return AjaxResult.SUCCESS(projectResourceService.getAllProjectResourceByProjectId(projectId, stageId));
    }

    /**
     * 获得资源上传链接
     *
     * @param projectId
     * @param resourceName
     * @return
     */
    @GetMapping("/getSign")
    public AjaxResult<CosPolicyVo> getSign(@RequestParam("projectId") int projectId, @RequestParam("stageId") int stageId, @RequestParam("resourceName") String resourceName){
        //( 提交阶段 && 项目未提交 ) ||  (项目被退回 && 审核阶段)
        CompetitionStageStatus singleSubmitCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(stageId, StatusConstant.COMPETITION_STAGE_STATUS_ORDER_SUBMIT.getCode());
        CompetitionStageStatus singleJudgeCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(stageId, StatusConstant.COMPETITION_STAGE_STATUS_ORDER_JUDGE.getCode());
        CheckUtil.checkStageStatus(singleSubmitCompStageStatus, singleJudgeCompStageStatus, projectService.getProjectStuStatus(projectId));

        return AjaxResult.SUCCESS(projectResourceService.getSign(projectId, stageId, resourceName));
    }

    /**
     * 新增项目资源
     *
     * @param projectId
     * @param resourceName
     * @return
     */
    @PostMapping("/addProjectResource")
    public AjaxResult<Integer> addProjectResource(@RequestParam("projectId") int projectId, @RequestParam("stageId") int stageId, @RequestParam("cosPath") String cosPath, @RequestParam("resourceName") String resourceName) {
        //( 提交阶段 && 项目未提交 ) ||  (项目被退回 && 审核阶段)
        CompetitionStageStatus singleSubmitCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(stageId, StatusConstant.COMPETITION_STAGE_STATUS_ORDER_SUBMIT.getCode());
        CompetitionStageStatus singleJudgeCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(stageId, StatusConstant.COMPETITION_STAGE_STATUS_ORDER_JUDGE.getCode());
        CheckUtil.checkStageStatus(singleSubmitCompStageStatus, singleJudgeCompStageStatus, projectService.getProjectStuStatus(projectId));

        return AjaxResult.SUCCESS(projectResourceService.addProjectResource(projectId, stageId, cosPath, resourceName));
    }


    /**
     * 更新项目资源
     *
     * @param projectResourceId
     * @param projectResourceName
     * @param cosPath
     */
    @Deprecated
    @PutMapping("/updateProjectResource")
    public AjaxResult<CosPolicyVo> updateProjectResourceById(@RequestParam("projectResourceId") int projectResourceId,@RequestParam("projectResourceName") String projectResourceName,@RequestParam("cosPath") String cosPath) {
        //项目资源存在
        ProjectResource projectResource = projectResourceService.getProjectResourceById(projectResourceId);
        //( 提交阶段 && 项目未提交 ) ||  (项目被退回 && 审核阶段)
        CompetitionStageStatus singleSubmitCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(projectResource.getStageId(), StatusConstant.COMPETITION_STAGE_STATUS_ORDER_SUBMIT.getCode());
        CompetitionStageStatus singleJudgeCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(projectResource.getStageId(), StatusConstant.COMPETITION_STAGE_STATUS_ORDER_JUDGE.getCode());
        CheckUtil.checkStageStatus(singleSubmitCompStageStatus, singleJudgeCompStageStatus, projectService.getProjectStuStatus(projectResource.getProjectId()));

        return AjaxResult.SUCCESS(projectResourceService.updateProjectResourceById(projectResourceId, projectResourceName, cosPath));
    }

    /**
     * 根据项目资源id获取项目资源下载链接
     *
     * @param projectResourceId 项目资源id
     * @return 项目资源
     */
    @GetMapping("/getProjectResource")
    public AjaxResult<CosPolicyVo> getProjectResourceById(@RequestParam("projectResourceId") int projectResourceId) {
        return AjaxResult.SUCCESS(projectResourceService.getDownloadById(projectResourceId));
    }

    /**
     * 根据项目资源id删除项目资源
     *
     * @param projectResourceId 项目资源id
     * @return 删除条数
     */
    @DeleteMapping("/deleteProjectResource")
    public AjaxResult<Integer> deleteProjectResourceById(@RequestParam("projectResourceId") int projectResourceId) {
        //项目资源存在
        ProjectResource projectResource = projectResourceService.getProjectResourceById(projectResourceId);
        //( 提交阶段 && 项目未提交 ) ||  (项目被退回 && 审核阶段)
        CompetitionStageStatus singleSubmitCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(projectResource.getStageId(), StatusConstant.COMPETITION_STAGE_STATUS_ORDER_SUBMIT.getCode());
        CompetitionStageStatus singleJudgeCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(projectResource.getStageId(), StatusConstant.COMPETITION_STAGE_STATUS_ORDER_JUDGE.getCode());
        CheckUtil.checkStageStatus(singleSubmitCompStageStatus, singleJudgeCompStageStatus, projectService.getProjectStuStatus(projectResource.getProjectId()));

        return AjaxResult.SUCCESS(projectResourceService.deleteProjectResourceById(projectResourceId));
    }

    /**
     * 新增项目资源 with bucket
     *
     * @param projectId
     * @param resourceName
     * @return
     */
    @Deprecated
    @PostMapping("/addProjectResourceWithBucket")
    public AjaxResult<CosPostPolicyVo> addProjectResourceWithBucket(@RequestParam("projectId") int projectId, @RequestParam("stageId") int stageId, @RequestParam("resourceName") String resourceName) {
        return AjaxResult.SUCCESS(projectResourceService.addProjectResourceWithBucket(projectId, stageId, resourceName));
    }

}
